<?php
/**
 * @purpose 购物车
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/28
 * Time: 上午9:31
 */

namespace app\mall\logic;

use app\model\GoodsAttributeAttach;
use app\model\ShoppingCart;
use extend\Functions;
use extend\STATUS_CODE;

class Cart
{
    /**
     * @purpose 获取购物车商品数量
     * @param int $uid
     * @return array
     */
    public static function total (int $uid): array
    {
        $model      = new ShoppingCart();
        $total      = $model
            ->where('uid', $uid)
            ->sum('number');
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'total' => $total
            ]
        );
    }

    /**
     * @purpose 获取购物车列表
     * @param int $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function item (int $uid) {
        $model      = new ShoppingCart();
        $result     = $model
//            ->fetchSql(true)
            ->alias('sc')
            ->join('goods g', 'sc.goods_id=g.id')
            ->join('goods_attribute_attach gaa', 'sc.goods_number=gaa.number and sc.goods_id=gaa.goods_id')
            ->where('sc.uid', $uid)
            ->field('sc.id,sc.goods_id,sc.goods_price,sc.goods_number,sc.number,sc.goods_title,sc.goods_spec,sc.goods_thumb,g.status,gaa.price as real_price')
            ->order('sc.create_at desc')
            ->select();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }
    /**
     * @purpose 加入购物车
     * @param array $data
     * @param int $uid
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function add (
        int $goods_id,
        string $goods_number = '',
        int $number,
        int $uid) :array
    {

        $_data              = [];
        $_data['uid']       = $uid;
        $_data['goods_id']  = $goods_id;
        if ($goods_number) {
            $goods_number   =  Functions::str_sort($goods_number);
        }
        $_data['goods_number']  = $goods_number;
        // 检测当前商品是否存在购物车,存在则商品加上商品数量,不存在则创建
        $model  =   new ShoppingCart();
        if ($model->total($_data) > 0) {
            $result = $model
                    ->where($_data)
                    ->setInc('number', $number);
            if ($result) {
                return Functions::result(
                    STATUS_CODE::SUCCESS,
                    '加入成功!'
                );
            }
            return Functions::result(
                STATUS_CODE::UPDATE_FAIL,
                '加入失败'
            );
        }
        $_data['number']            = $number;
        $goods                      = new \app\model\Goods();
        $goodsAttributeAttach       = new GoodsAttributeAttach();

        $goods_info  = $goods
            ->where('id', $goods_id)
            ->field('price,thumb,title')
            ->find();
        $goods_attach       = $goodsAttributeAttach
            ->where(['goods_id' => $goods_id, 'number' => $goods_number])
            ->field('price,thumb,attach')
            ->find();
        $_data['goods_title']     = $goods_info->title;
        if ($goods_attach) {
            $_data['goods_price'] = $goods_attach->price;
            $_data['goods_thumb'] = $goods_attach->thumb;
            $_data['goods_spec']  = '';
            foreach ($goods_attach->attach as $v) {
                $_data['goods_spec'] .= ($v->name). ' ';
            }
        } else {
            $_data['goods_price'] = $goods_info->price;
            $_data['goods_thumb'] = $goods_info->thumb;
        }
        $result = $model
            ->isUpdate(false)
            ->save($_data);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '加入成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::UPDATE_FAIL,
            '加入失败'
        );

    }

    /**
     * @purpose 从购物车中删除
     * @param array $ids
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public static function del (array $ids, int $uid): array
    {
        $model  =  new ShoppingCart();
        $result =  $model
            ->where([
                'uid' => $uid,
                'id'  => $ids
            ])
            ->delete();
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '删除成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::DELETE_FAIL,
            '删除失败'
        );
    }

    /**
     * @purpose 改变购物车商品数量
     * @param int $id
     * @param int $number
     * @param int $uid
     * @return array
     */
    public static function changeNumber (int $id, int $number, int $uid): array
    {
        $model      = new ShoppingCart();
        $result     = $model
            ->save(
                ['number' => $number],
                ['id' => $id, 'uid' => $uid]);

        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                'success'
            );
        }
        return Functions::result(
            STATUS_CODE::DELETE_FAIL,
            '更新失败'
        );
    }
}